package ${package.ServiceImpl};

import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hoshiibuy.common.aop.Logable;
import com.hoshiibuy.common.exception.CommonError;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.${package.ModuleName}.feign.IdGeneratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;

/**
 * <p>
 * ${table.comment!} 服务实现类
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Service
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {

}
<#else>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {

    @Autowired
    private IdGeneratorService idGeneratorService;

    /**
    * @param id
    * @return com.hoshiibuy.${package.ModuleName}.entity.${entity}
    * @description: 根据id查询${table.comment!}
    */
    @Cacheable(cacheNames = "${entity}", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "${cfg.mchname}-${table.comment!} -查询${table.comment!} ")
    public ${entity} getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
    * @param entity
    * @return boolean
    * @description: 新增${table.comment!}
    */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "${entity}", allEntries = true)
    @Logable(authName = "${cfg.mchname}-${table.comment!}-新增${table.comment!}")
    public boolean save(${entity} entity) {
        String id = idGeneratorService.queryGeneratorId();
        if (id == null || id == "") {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedtime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    /**
    * @param entity
    * @return boolean
    * @description: 修改${table.comment!}
    */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "${entity}", allEntries = true)
    @Logable(authName = "${cfg.mchname}-${table.comment!}管理-修改${table.comment!}")
    public boolean updateById(${entity} entity) {
        entity.setUpdatedtime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    /**
    * @param queryWrapper
    * @return IPage
    * @description: 分页查询${table.comment!}
    */
    @Logable(authName = "${cfg.mchname}-${table.comment!}管理-分页查询${table.comment!}")
    @Cacheable(cacheNames = "${entity}", key = "T(com.hoshiibuy.common.utils.DigestUtils).md5('Page(current=' + #page.getCurrent() + ', size=' + #page.getSize() + '), Wrapper(SqlSelect=' + #queryWrapper.getSqlSelect() + ', SqlSegment=' + #queryWrapper.getSqlSegment() + ')')")
    public IPage<${entity}> page(IPage<${entity}> page, Wrapper<${entity}> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }
}
</#if>
